// merchants/cardDetails/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, //0会员卡详情;1优惠券详情;2消费卡详情
    vipCard: {},
    couponInfo: {},
    cardItem: {},
    detailpics: []
  },

  // 我的会员卡详情
  getVipcard: function (staffid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardByUser?staffId=' + staffid + '&uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          let vipCard = res.data.Data;
          if (vipCard) {
            let year = vipCard.EndTime.substr(0, 4);
            let month = vipCard.EndTime.substr(5, 2);
            let date = vipCard.EndTime.substr(8, 2);
            vipCard.EndTime = year + "/" + month + "/" + date;
            vipCard.Discount = vipCard.Discount * 0.1;
            vipCard.Discount = vipCard.Discount.toFixed(1);
            var detailpics = vipCard.DetailPics;
            var list = [];
            if (detailpics) {
              var newDetailPics = detailpics.split(",");
              for (let i = 0; i < newDetailPics.length; i++) {
                list[i] = 'https://static.711688.net.cn/upload/coupondetail/' + newDetailPics[i];
              }
            }
          }
          that.setData({
            vipCard: vipCard,
            detailpics: list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 优惠券详情
  getCouponDetail: function (couponid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponInfo?couponId=' + couponid + '&uid=' + app.globalData.userInfo.Uid + '&showuser=1',
      success: function (res) {
        if (res.data.Code == 0) {
          let couponInfo = res.data.Data;
          var detailpics = couponInfo.type.DetailPics;
          var list = [];
          if (detailpics != '') {
            var newDetailPics = detailpics.split(",");
            for (let i = 0; i < newDetailPics.length; i++) {
              list[i] = 'https://static.711688.net.cn/upload/coupondetail/' + newDetailPics[i];
            }
          }
          if (couponInfo) {
            couponInfo.Name = couponInfo.type.Name;
            couponInfo.OrderAmountLower = couponInfo.type.OrderAmountLower;
            couponInfo.Money = couponInfo.type.Money;
            couponInfo.ExtCode1 = couponInfo.type.ExtCode1;
            couponInfo.ExtCode2 = couponInfo.type.ExtCode2;
            couponInfo.Mobile = couponInfo.user.Mobile;
            couponInfo.Remark = couponInfo.type.Remark;
            // console.log(couponInfo.type.Remark.match(/(?=\#链接)/));//之前
            // console.log(couponInfo.type.Remark.match(/(?<=链接\#)/));//之后
            // let createUid = param.substring(param.indexOf('createUid=') + 10, param.indexOf('&id='));
            // let employeeidParam = param.substring(param.indexOf('employeeid=') + 11, param.length);
            let remark = couponInfo.type.Remark;
            if (remark && remark.indexOf('#链接') != -1) {
              let begin = remark.substr(0, remark.indexOf('#链接'));
              let end = remark.substr(remark.indexOf('链接#') + 3, remark.length);
              // couponInfo.Remark = remark.match(/(?=\#链接)/) + remark.match(/(?<=链接\#)/);//ios不支持
              couponInfo.Remark = begin + end;
            }
            if (couponInfo.type.UseExpireTime > 0) {
              couponInfo.ExpireTime = that.addDate(couponInfo.Coupon.ActivateTime, couponInfo.type.UseExpireTime);
            } else {
              couponInfo.ExpireTime = couponInfo.type.UseEndTime.substr(0, 10);
            }
          }
          that.setData({
            couponInfo: couponInfo,
            detailpics: list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  //商家获取优惠券详情
  getCouponDetailbyStaff: function (coupontypeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponTypeInfo?couponTypeId=' + coupontypeid + '&showMobile=1',
      success: function (res) {
        if (res.data.Code == 0) {
          let couponInfo = res.data.Data;
          var detailpics = couponInfo.DetailPics;
          var list = [];
          if (detailpics != '') {
            var newDetailPics = detailpics.split(",");
            for (let i = 0; i < newDetailPics.length; i++) {
              list[i] = 'https://static.711688.net.cn/upload/coupondetail/' + newDetailPics[i];
            }
          }
          if (res.data.Data) {
            couponInfo.ExpireTime = couponInfo.UseEndTime.substr(0, 10);
            couponInfo.Name = couponInfo.CouponTypeName;
          }
          that.setData({
            couponInfo: couponInfo,
            detailpics: list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 消费卡详情
  getCardItemDetail: function (cid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardUser?cid=' + cid,
      success: function (res) {
        if (res.data.Code == 0) {
          let cardItem = res.data.Data;
          var detailpics = cardItem.DetailPics;
          var list = [];
          if (detailpics != '') {
            var newDetailPics = detailpics.split(",");
            for (let i = 0; i < newDetailPics.length; i++) {
              list[i] = 'https://static.711688.net.cn/upload/coupondetail/' + newDetailPics[i];
            }
          }
          if (cardItem) {
            cardItem.ExpireTime = cardItem.ExpireTime.substr(0, 10);
          }
          that.setData({
            cardItem: cardItem,
            detailpics: list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 商家获取消费卡详情
  getCardItemDetailByStaff: function (id) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardItem?id=' + id,
      success: function (res) {
        if (res.data.Code == 0) {
          let cardItem = res.data.Data;
          var detailpics = cardItem.DetailPics;
          var list = [];
          if (detailpics != '') {
            var newDetailPics = detailpics.split(",");
            for (let i = 0; i < newDetailPics.length; i++) {
              list[i] = 'https://static.711688.net.cn/upload/coupondetail/' + newDetailPics[i];
            }
          }
          cardItem.ExpireTime = '领取后' + cardItem.Days + '天';
          that.setData({
            cardItem: cardItem,
            detailpics: list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },


  // 获取股东礼包
  getPackageInfo: function (createUid, shareUid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetPackageInfo?createUid=' + createUid + '&uid=' + shareUid,
      success: function (res) {
        if (res.data.Code == 0) {
          let packageInfo = res.data.Data;
          let card = {};
          if (packageInfo.card != null) {
            card = packageInfo.card;
            let year = card.EndTime.substr(0, 4);
            let month = card.EndTime.substr(5, 2);
            let date = card.EndTime.substr(8, 2);
            card.EndTime = year + "/" + month + "/" + date;
            card.Discount = (card.Discount * 0.1).toFixed(1);
          }
          that.setData({
            vipCard: card
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    if (options.type == 0) { //会员卡详情
      wx.setNavigationBarTitle({
        title: '会员卡详情'
      })
      if (options.createUid != 'undefined') {
        this.getPackageInfo(options.createUid, options.shareUid)
      } else if (options.staffid) {
        this.getVipcard(options.staffid);
      }
    }
    if (options.type == 1) { //优惠券详情
      wx.setNavigationBarTitle({
        title: '优惠券详情'
      })
      if (options.couponid && options.couponid != 'undefined') {
        this.getCouponDetail(options.couponid);
        this.setData({
          couponid: options.couponid
        })
      } else {
        this.getCouponDetailbyStaff(options.coupontypeid);
      }

    }
    if (options.type == 2) { //消费卡详情
      wx.setNavigationBarTitle({
        title: '消费卡详情'
      })
      let cid = 0;
      let ids = 0;
      if (options.cid > 0) {
        cid = options.cid
        this.getCardItemDetail(cid);
      } else {
        if (options.ids > 0) {
          ids = options.ids;
        }
        this.getCardItemDetailByStaff(ids);
      }
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //商品详情图片预览
  preViewDetailsImg: function (e) {
    let list = this.data.detailpics;
    let src = e.currentTarget.dataset.src;
    let imgs = [];
    for (let i = 0; i < list.length; i++) {
      imgs.push(list[i]);
    }
    imgs.join(",");
    wx.previewImage({
      current: src,
      urls: imgs,
    })
  },
  addDate: function (dataStr, dayCount) {
    var strdate = dataStr; //日期字符串
    var isdate = new Date(strdate.replace(/-/g, "/")); //把日期字符串转换成日期格式
    isdate = new Date((isdate / 1000 + (86400 * dayCount)) * 1000); //日期加1天
    var pdate = isdate.getFullYear() + "-" + (isdate.getMonth() + 1) + "-" + (isdate.getDate());
    return pdate;
  }
})